//
//  CYProjectDetailViewController.m
//  cyydt
//
//  Created by chu on 2019/1/24.
//  Copyright © 2019 chu. All rights reserved.
//

#import "CYProjectDetailViewController.h"

#import "CYProjectDetail_XiangMuJieShaoViewController.h"
#import "CYProjectDetail_ZhaoShangXiangQingViewController.h"
#import "CYProjectDetail_PinPaiDongTaiViewController.h"

@interface CYProjectDetailViewController ()<SGTopScrollMenuDelegate, UIScrollViewDelegate>

@property (nonatomic, strong) SGTopScrollMenu *topMenu;

@property (nonatomic, strong) UIScrollView *mainScrollView;

@property (nonatomic, strong) UIView *titleView;

@end

@implementation CYProjectDetailViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
//    IQKeyboardManager *manager = [IQKeyboardManager sharedManager];
//    manager.enable = NO;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
//    IQKeyboardManager *manager = [IQKeyboardManager sharedManager];
//    manager.enable = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    // 创建底部滚动视图
    self.mainScrollView = [[UIScrollView alloc] init];
    _mainScrollView.frame = CGRectMake(0, 44, kWindowW, kWindowH - Height_NavBar - 44);
    _mainScrollView.contentSize = CGSizeMake(self.view.frame.size.width * 3, 0);
    _mainScrollView.backgroundColor = [UIColor clearColor];
    // 开启分页
    _mainScrollView.pagingEnabled = YES;
    // 没有弹簧效果
    _mainScrollView.bounces = NO;
    // 隐藏水平滚动条
    _mainScrollView.showsHorizontalScrollIndicator = NO;
    // 设置代理
    _mainScrollView.delegate = self;
    [self.view addSubview:_mainScrollView];
    
    [self setChildViewControllers];
    
    [self.view addSubview:self.topMenu];
    
    self.navigationItem.titleView = self.titleView;
}

// 显示控制器的view
- (void)showVc:(NSInteger)index {
    
    CGFloat offsetX = index * self.view.frame.size.width;
    
    UIViewController *vc = self.childViewControllers[index];
    // 1 计算滚动的位置
    self.mainScrollView.contentOffset = CGPointMake(offsetX, 0);
    
    // 判断控制器的view有没有加载过,如果已经加载过,就不需要加载
    if (vc.isViewLoaded) return;
    
    [self.mainScrollView addSubview:vc.view];
    vc.view.frame = CGRectMake(offsetX, 0, self.view.frame.size.width, self.mainScrollView.frame.size.height);
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    // 计算滚动到哪一页
    NSInteger index = scrollView.contentOffset.x / scrollView.bounds.size.width;
    // 1.添加子控制器view
    [self showVc:index];
    [self.topMenu selectLabel:self.topMenu.allTitleLabel[index]];
}

#pragma mark - SGTopScrollMenuDelegate
- (void)SGTopScrollMenu:(SGTopScrollMenu *)topScrollMenu didSelectTitleAtIndex:(NSInteger)index{
    [self showVc:index];
    
}

- (void)setChildViewControllers{
    CYProjectDetail_XiangMuJieShaoViewController *commend = [[CYProjectDetail_XiangMuJieShaoViewController alloc] init];
    commend.model = self.model;
    commend.isFromVideoDetail = self.isFromVideoDetail;
    commend.view.frame = CGRectMake(0, 0, kWindowW, self.mainScrollView.frame.size.height);
    [self.mainScrollView addSubview:commend.view];
    [self addChildViewController:commend];
    
    CYProjectDetail_ZhaoShangXiangQingViewController *info = [[CYProjectDetail_ZhaoShangXiangQingViewController alloc] init];
    info.model = self.model;
    info.isFromVideoDetail = self.isFromVideoDetail;
    info.view.frame = CGRectMake(kWindowW, 0, kWindowW, self.mainScrollView.frame.size.height);
    [self.mainScrollView addSubview:info.view];
    [self addChildViewController:info];
    
    CYProjectDetail_PinPaiDongTaiViewController *video = [[CYProjectDetail_PinPaiDongTaiViewController alloc] init];
    video.model = self.model;
    video.isFromVideoDetail = self.isFromVideoDetail;
    [self addChildViewController:video];
    
}

- (SGTopScrollMenu *)topMenu{
    if (!_topMenu) {
        _topMenu = [SGTopScrollMenu topScrollMenuWithFrame:CGRectMake(0, 0, kWindowW , 44) NormalColor:RGBOF(0x999999) SelColor:RGBOF(0xF43636)];
        _topMenu.backgroundColor = RGBOF(0xf7f7f7);
        _topMenu.topScrollMenuDelegate = self;
        _topMenu.titlesArr = @[@"项目介绍", @"招商详情", @"品牌动态"];
    }
    return _topMenu;
}

- (UIView *)titleView{
    if (!_titleView) {
        _titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kWindowW - 40, 44)];
        UIImageView *logo = [[UIImageView alloc] initWithFrame:CGRectMake(0, 5, 120, 34)];
        if (kWindowW > 321) {
            [_titleView addSubview:logo];
        }else{
            logo.frame = CGRectMake(0, 5, 80, 34);
        }
        logo.image = [UIImage imageNamed:@"logo"];

        NSArray *titles = @[@"找项目", @"读资讯", @"看视频"];
        for (int i = 0; i < titles.count; i++) {
            UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetMaxX(logo.frame) + 30 + 40 * i + i * 10, 0, 43, 44)];
            [btn setTitle:titles[i] forState:UIControlStateNormal];
            [btn addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];
            btn.tag = i;
            [btn setTitleColor:RGBOF(0x333333) forState:UIControlStateNormal];
            btn.titleLabel.font = [UIFont systemFontOfSize:13];
            [_titleView addSubview:btn];
        }
    }
    return _titleView;
}

- (void)clickAction:(UIButton *)sender{
    if (sender.tag == 0) {
        //找项目
        [self.tabBarController setSelectedIndex:1];

        [self.navigationController popToRootViewControllerAnimated:NO];
    }else if (sender.tag == 1){
        //读资讯
        [self.tabBarController setSelectedIndex:0];
        [self.navigationController popToRootViewControllerAnimated:NO];

        [[NSNotificationCenter defaultCenter] postNotificationName:@"gotoZiXun" object:nil];

    }else{
        //看视频
        [self.tabBarController setSelectedIndex:2];
        
        [self.navigationController popToRootViewControllerAnimated:NO];
    }
}

@end
